package tech.heptagram.staff.cas.config.url;


import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.PostConstruct;

import lombok.extern.slf4j.Slf4j;
import tech.heptagram.staff.cas.common.AssertUtils;

/**
 * @author zilaiye
 * @date 2017/12/15
 */
@Slf4j
public abstract class AbstractUrlService implements UrlService {
    private String casHost;
//    private String staffHost;
    private String alkaidHost;

    @Value("${app.domain}")
    private String clientDomain;
    @Value("${server.servlet.context-path}")
    private String contextPath;

    // alkaid api
    private String findAllCitiesApiUrl;

    // staff api url
//    private String staffFindAllApiUrl;
//    private String staffFindAllListApiUrl;
//    private String staffFindApiUrl;

    // cas server  url
    private String casServerHostUrl;
    private String casServerLoginUrl;
    private String casServerLogoutUrl;

    // cas client app url
    private String casClientLoginUrl;
    private String casClientLogoutUrl;
    private String casClientCallbackUrl;


    public AbstractUrlService() {

    }

    public AbstractUrlService(String casHost, String staffHost, String alkaidHost) {
        this.casHost = casHost;
//        this.staffHost = staffHost;
        this.alkaidHost = alkaidHost;

        AssertUtils.hasText(casHost, "cas server domain 没有设置");
        AssertUtils.hasText(staffHost, "staff 域名未设置");
        AssertUtils.hasText(alkaidHost, "alkaid 域名未设置");

        log.info("use the cas-server-domain: {}", casHost);
        log.info("use the staff-domain:{}", staffHost);
        log.info("use the alkaid-domain:{}", alkaidHost);
    }

    @PostConstruct
    public void initUrl() {
        log.info("client domain:{} context-path:{}", clientDomain, contextPath);
        if (StringUtils.isEmpty(contextPath)) {
            this.contextPath = "";
        }
        if("/".equals(contextPath)){
            this.contextPath = "";
        }
        AssertUtils.hasText(clientDomain, "请在application.yml中配置此系统的域名, 配置节点: app domain 配置值格式:https://www.zhongfl.com");
        log.info("use the cas-client-domain: {}", clientDomain);

        // AlkaidApi
        this.findAllCitiesApiUrl = alkaidHost + "/api/v1/area/findAllCitys";

        // StaffApi
//        this.staffFindAllApiUrl = staffHost + "/api/v1/staff/findStaffPageInfo";
//        this.staffFindAllListApiUrl = staffHost + "/api/v1/staff/findAllStaff";
//        this.staffFindApiUrl = staffHost + "/api/v1/staff/findStaff";

        String casClientHostUrl = getCasClientHostUrl();
        // Cas Server Url
        this.casServerHostUrl = casHost;
        this.casServerLoginUrl = casHost + "/login";
        this.casServerLogoutUrl = casHost + "/logout?service=" + casClientHostUrl + "/index";

        // Cas Client Url
        this.casClientLoginUrl = "/login";
        this.casClientLogoutUrl = "/logout";
        this.casClientCallbackUrl = casClientHostUrl + "/system/cas/callback";

        logUrlMapping();
    }

    private void logUrlMapping(){
        log.info("Mapped \"{[{}]}\" onto findAllCitiesApiUrl", this.findAllCitiesApiUrl);

//        log.info("Mapped \"{[{}]}\" onto staffFindAllApiUrl", this.staffFindAllApiUrl);
//        log.info("Mapped \"{[{}]}\" onto staffFindAllListApiUrl", this.staffFindAllListApiUrl);
//        log.info("Mapped \"{[{}]}\" onto staffFindApiUrl", this.staffFindApiUrl);

        log.info("Mapped \"{[{}]}\" onto casServerHostUrl", this.casServerHostUrl);
        log.info("Mapped \"{[{}]}\" onto casServerLoginUrl", this.casServerLoginUrl);
        log.info("Mapped \"{[{}]}\" onto casServerLogoutUrl", this.casServerLogoutUrl);

        log.info("Mapped \"{[{}]}\" onto casClientLoginUrl", this.casClientLoginUrl);
        log.info("Mapped \"{[{}]}\" onto casClientLogoutUrl", this.casClientLogoutUrl);
        log.info("Mapped \"{[{}]}\" onto casClientCallbackUrl", this.casClientCallbackUrl);
    }

    /**
     * 获取所有城市接口url
     *
     * @return
     */
    @Override
    public String getFindAllCitiesApiUri() {
        return this.findAllCitiesApiUrl;
    }

//    @Override
//    public String getStaffHost() {
//        return this.staffHost;
//    }
//
//    @Override
//    public String getStaffFindAllApiUrl() {
//        return this.staffFindAllApiUrl;
//    }
//
//    @Override
//    public String getStaffFindAllListApiUrl() {
//        return this.staffFindAllListApiUrl;
//    }
//
//    @Override
//    public String getStaffFindApiUrl() {
//        return this.staffFindApiUrl;
//    }

    @Override
    public String getCasServerHostUrl() {
        return this.casServerHostUrl;
    }

    /**
     * cas服务器登录Uri
     *
     * @return cas server login uri
     */
    @Override
    public String getCasServerLoginUrl() {
        return this.casServerLoginUrl;
    }

    /**
     * cas server logout uri
     *
     * @return cas server logout uri
     */
    @Override
    public String getCasServerLogoutUrl() {
        return this.casServerLogoutUrl;
    }

    @Override
    public String getCasClientLoginUrl() {
        return this.casClientLoginUrl;
    }

    @Override
    public String getCasClientLogoutUrl() {
        return this.casClientLogoutUrl;
    }

    @Override
    public String getCasClientCallbackUrl() {
        return this.casClientCallbackUrl;
    }

    private String getCasClientHostUrl() {
        StringBuilder sb = new StringBuilder();
        sb.append(clientDomain);

//        if (clientServerPort != DEFAULT_PORT) {
//            sb.append(":").append(clientServerPort);
//        }
        sb.append(contextPath);
        return sb.toString();
    }
}
